<?php

namespace app\data\logic;

use app\data\service\JobdepartmentService;
use app\data\service\JobpositionService;

class JobLogic extends BaseLogic
{
    protected JobdepartmentService $jobdepartmentService;
    protected JobpositionService $jobpositionService;

    public function __construct()
    {
        $this->jobdepartmentService = new JobdepartmentService();
        $this->jobpositionService = new JobpositionService();
    }

    /**
     * 部门列表
     * @param $params
     * @return array
     */
    public function getJobDepartmentList($params)
    {
        $field = '*';
        $where = [];
        $order = 'sort_order desc,department_id desc';
        $withs = ['jobpositions'];
        $pageIndex = $params['page'] ?? 1;
        $pageSize = $params['limit'] ?? 10;
        $result = $this->jobdepartmentService->getPageList($pageIndex, $pageSize, $where, $field, $order, $withs);
        if (array_key_exists('all', $params)) {
            $result = $this->jobdepartmentService->getAll($field, $where, $order, $withs);
        }
        return $result;
    }


    /**
     * 添加部门
     * @param $data
     * @return array
     */
    public function createNewJobDepartment($data): array
    {
        try {
            $ret = $this->jobdepartmentService->insert($data);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 更新部门
     * @param $id
     * @param $data
     * @return array
     */
    public function updateJobDepartment($id, $data): array
    {
        try {
            $where = [
                'department_id' => $id
            ];
            $ret = $this->jobdepartmentService->update($where, $data);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 职位部门
     * @param $id
     * @return array
     */
    public function getJobDepartmentDetail($id): array
    {
        try {
            $where = [
                'department_id' => $id
            ];
            $withs = ['jobpositions'];
            $res = $this->jobdepartmentService->getOne('*', $where, $withs);
            if ($res) {
                return $this->responseInfo(true, 'SUCCESS', $res);
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 删除部门
     * @param $id
     * @return array
     */
    public function deleteJobDepartment($id): array
    {
        try {
            $where = [
                'department_id' => $id
            ];
            $ret = $this->jobdepartmentService->softDel($where);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }


    /**
     * 职位分类列表
     * @param $params
     * @return array
     */
    public function getJobpositionList($params)
    {
        $field = '*';
        $where = [];
        $order = 'sort_order desc,position_id desc';
        $withs = ['department'];
        $pageIndex = $params['page'] ?? 1;
        $pageSize = $params['limit'] ?? 10;
        $result = $this->jobpositionService->getPageList($pageIndex, $pageSize, $where, $field, $order, $withs);
        if (array_key_exists('all', $params)) {
            $result = $this->jobpositionService->getAll($field, $where, $order, $withs);
        }

        return $result;
    }


    /**
     * 添加职位
     * @param $data
     * @return array
     */
    public function createNewJobposition($data): array
    {
        try {
            $ret = $this->jobpositionService->insert($data);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 更新职位
     * @param $id
     * @param $data
     * @return array
     */
    public function updateJobposition($id, $data): array
    {
        try {
            $where = [
                'position_id' => $id
            ];
            $ret = $this->jobpositionService->update($where, $data);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 职位详情
     * @param $id
     * @return array
     */
    public function getJobpositionDetail($id): array
    {
        try {
            $where = [
                'position_id' => $id
            ];
            $withs = ['department'];
            $res = $this->jobpositionService->getOne('*', $where, $withs);
            if ($res) {
                return $this->responseInfo(true, 'SUCCESS', $res);
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 删除职位
     * @param $id
     * @return array
     */
    public function deleteJobposition($id): array
    {
        try {
            $where = [
                'position_id' => $id
            ];
            $ret = $this->jobpositionService->softDel($where);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }


}